package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.LineBaseBigType;
import com.sailfish.springbootdemo.pojo.db1.LineBaseSmallType;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface LineBaseSmallTypeDao {

    /**
     * 根据 大类 查询所有
     *
     * @param searchValue
     * @param lineBaseBigTypeId
     * @return
     */
    @Results(id = "lineBaseSmallTypeResultMap", value = {
            @Result(column = "line_base_small_type_id", property = "lineBaseSmallTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "line_base_small_type_name", property = "lineBaseSmallTypeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "line_base_big_type_id", property = "lineBaseBigTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_process_id", property = "pcbaProcessId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_process_id_smt", property = "pcbaProcessIdSmt", jdbcType = JdbcType.INTEGER),
    })
    @Select("<script>" +
            "select * from " + DB1TableName.LINE_BASE_SMALL_TYPE + " where 1 = 1 " +
            "<if test='searchValue != null and searchValue != \"\" '>" +
            " and line_base_small_type_name like CONCAT('%',#{searchValue},'%')" +
            "</if>" +
            "<if test='lineBaseBigTypeId != null and lineBaseBigTypeId != \"\" '>" +
            " and line_base_big_type_id = #{lineBaseBigTypeId}" +
            "</if>" +
            " order by sort" +
            "</script>")
    List<LineBaseSmallType> getAllLineBaseSmallTypeByBigType(@Param("searchValue") String searchValue, @Param("lineBaseBigTypeId") Integer lineBaseBigTypeId);

    @ResultMap(value = "lineBaseSmallTypeResultMap")
    @Select("select * from " + DB1TableName.LINE_BASE_SMALL_TYPE + " where line_base_big_type_id = #{lineBaseBigTypeId} and line_base_small_type_name = #{lineBaseSmallTypeName}")
    List<LineBaseSmallType> getLineBaseSmallTypeByNameAndBigType(@Param("lineBaseSmallTypeName") String lineBaseSmallTypeName, @Param("lineBaseBigTypeId") Integer lineBaseBigTypeId);

    /**
     * 根据processId查询
     */
    @Select("select * from " + DB1TableName.LINE_BASE_SMALL_TYPE + " where pcba_process_id = #{processId}")
    @ResultMap(value = "lineBaseSmallTypeResultMap")
    List<LineBaseSmallType> getLineBaseSmallTypeByProcessId(@Param("processId") Integer processId);

    /**
     * 根据id查询
     *
     * @param lineBaseSmallTypeId
     * @return
     */
    @Select("select * from " + DB1TableName.LINE_BASE_SMALL_TYPE + " where line_base_small_type_id = #{lineBaseSmallTypeId}")
    @ResultMap(value = "lineBaseSmallTypeResultMap")
    LineBaseSmallType getLineBaseSmallTypeById(@Param("lineBaseSmallTypeId") Integer lineBaseSmallTypeId);

    /**
     * 判断有没有重复的
     *
     * @return
     */
    @ResultMap(value = "lineBaseSmallTypeResultMap")
    @Select("<script>" +
            "select * from " + DB1TableName.LINE_BASE_SMALL_TYPE + " where line_base_small_type_name = #{lineBaseSmallTypeName} and line_base_big_type_id = #{lineBaseBigTypeId} " +
            "<if test='lineBaseSmallTypeId != null and lineBaseSmallTypeId != \"\" '>" +
            " and line_base_small_type_id != #{lineBaseSmallTypeId}" +
            "</if>" +
            "</script>")
    List<LineBaseSmallType> judgeUniqueLineBaseSmallType(@Param("lineBaseBigTypeId") Integer lineBaseBigTypeId, @Param("lineBaseSmallTypeId") Integer lineBaseSmallTypeId, @Param("lineBaseSmallTypeName") String lineBaseSmallTypeName);


    /**
     * 新增
     *
     * @param lineBaseSmallType
     * @return
     */
    @Insert("insert into " + DB1TableName.LINE_BASE_SMALL_TYPE + "(line_base_small_type_name,status,sort,create_time,line_base_big_type_id,pcba_process_id,pcba_process_id_smt) values " +
            "(#{lineBaseSmallType.lineBaseSmallTypeName},#{lineBaseSmallType.status},#{lineBaseSmallType.sort},#{lineBaseSmallType.createTime},#{lineBaseSmallType.lineBaseBigTypeId},#{lineBaseSmallType.pcbaProcessId},#{lineBaseSmallType.pcbaProcessIdSmt})")
    Integer addLineBaseSmallType(@Param("lineBaseSmallType") LineBaseSmallType lineBaseSmallType);

    /**
     * 更新
     *
     * @param lineBaseSmallType
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.LINE_BASE_SMALL_TYPE +
            "<set>" +
            "<if test='lineBaseSmallType.lineBaseSmallTypeName != null and lineBaseSmallType.lineBaseSmallTypeName != \"\" '>" +
            " line_base_small_type_name = #{lineBaseSmallType.lineBaseSmallTypeName}," +
            "</if>" +
            "<if test='lineBaseSmallType.lineBaseBigTypeId != null and lineBaseSmallType.lineBaseBigTypeId != \"\" '>" +
            " line_base_big_type_id = #{lineBaseSmallType.lineBaseBigTypeId}," +
            "</if>" +
            "<if test='lineBaseSmallType.pcbaProcessId != null and lineBaseSmallType.pcbaProcessId != \"\" '>" +
            " pcba_process_id = #{lineBaseSmallType.pcbaProcessId}," +
            "</if>" +
            "<if test='lineBaseSmallType.pcbaProcessIdSmt != null and lineBaseSmallType.pcbaProcessIdSmt != \"\" '>" +
            " pcba_process_id_smt = #{lineBaseSmallType.pcbaProcessIdSmt}," +
            "</if>" +
            "<if test='lineBaseSmallType.sort != null and lineBaseSmallType.sort != \"\" '>" +
            " sort = #{lineBaseSmallType.sort}," +
            "</if>" +
            "<if test='lineBaseSmallType.updateTime != null '>" +
            " update_time = #{lineBaseSmallType.updateTime}," +
            "</if>" +
            "</set>" +
            " where line_base_small_type_id = #{lineBaseSmallType.lineBaseSmallTypeId}" +
            "</script>")
    Integer updateLineBaseSmallType(@Param("lineBaseSmallType") LineBaseSmallType lineBaseSmallType);

    /**
     * 删除
     *
     * @param lineBaseSmallTypeId
     * @return
     */
    @Delete("delete from " + DB1TableName.LINE_BASE_SMALL_TYPE + " where line_base_small_type_id = #{lineBaseSmallTypeId}")
    Integer deleteLineBaseSmallType(@Param("lineBaseSmallTypeId") Integer lineBaseSmallTypeId);
}
